package com.loong.service.impl;

import com.loong.entity.WordBook;
import com.loong.mapper.WordBookMapper;
import com.loong.service.IWordBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author loong
 * @since 2024-12-08
 */
@Service
public class WordBookServiceImpl extends ServiceImpl<WordBookMapper, WordBook> implements IWordBookService {

    @Override
    public List<List<WordBook>> category(List<WordBook> wordBooks) {
        List<List<WordBook>> result = new ArrayList<>();
        int lineNumber = 4;
        int i = 0;
        for (; !wordBooks.isEmpty()&&i <= wordBooks.size() / lineNumber; i++) {
            result.add(new LinkedList<>());
        }
        // 第一个list只包含lineNumber-1个元素
        if (!result.isEmpty()) {
            i = 0;
            for (; i < lineNumber - 1 && i< wordBooks.size(); i++) {
                result.get(0).add(wordBooks.get(i));
            }
        }else{
            return result;
        }
        // 剩下的每一个list包含lineNumber-1个元素
        while (i < wordBooks.size()) {
            int row = (i - lineNumber + 1) / lineNumber + 1;
            result.get(row).add(wordBooks.get(i));
            i++;
        }
        return result;
    }
}
